package cn.itheima.controller.company;

import cn.itheima.controller.base.BaseComtroller;
import cn.itheima.domain.Company;
import cn.itheima.domain.PageBean;
import cn.itheima.service.company.CompanyService;
import cn.itheima.util.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/company")
public class CompanyController extends BaseComtroller {
    @Autowired
    private CompanyService companyService;

    @RequestMapping(name = "查询企业",value = "/list2")
    public String findAll(){
        List<Company> list = companyService.findAll();
        request.setAttribute("list",list);
        return "/company/company-list";
    }

    @RequestMapping(name = "查询企业",value = "/list")
    public String findAll2(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize){
        PageInfo pb = companyService.findAll2(pageNum, pageSize);
        request.setAttribute("pb",pb);

        return "/company/company-list";
    }


    @RequestMapping(name = "去添加企业",value = "/toAdd")
    public String toAdd(){
        return "/company/company-add";
    }
    @RequestMapping(name = "添加和修改企业信息",value = "/edit")
    public String edit(Company company){
        if (UtilFuns.isEmpty(company.getId())){
            String id = UUID.randomUUID().toString();
            company.setId(id);
            companyService.edit(company);
            return "redirect:/company/list.do";
        }else {
            companyService.Update(company);
            return "redirect:/company/list.do";
        }

    }

    @RequestMapping(name = "查询企业",value = "/toUpdate")
    public String toUpdate(String id){
        Company company=companyService.toUpdate(id);
        request.setAttribute("company",company);

        return "company/company-update";
    }

    @RequestMapping(name = "删除企业",value = "/delete")
    public String delete(String id){
        System.out.println(id);
        companyService.delete(id);

        return "redirect:/company/list.do";
    }

}
